package com.xysq;

import android.app.Application;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xysq.lemon.R;
import com.xysq.util.DBHelper;
import com.xysq.util.PropertiesUtil;

/**
 * Created by Steward_Han on 2015/7/13.
 * This file is created to add loginName to application
 */
public class XYApplication extends Application {
    private String loginName;
    private static ImageLoader imageLoader;
    private static final int DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50 MB
    private DBHelper dbHelper;
    private static IWXAPI msgApi;

    @Override
    public void onCreate() {
        super.onCreate();
        PropertiesUtil.init(this);
        //初始化ImageLoader
        initImageLoader();
        dbHelper = new DBHelper(this,DBHelper.DBNAME,null, DBHelper.version);
        dbHelper.createTable(dbHelper.getWritableDatabase());
        // 将该app注册到微信
        msgApi = WXAPIFactory.createWXAPI(this, null);
    }

    //初始化ImageLoader
    private void initImageLoader() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(com.xysq.lemon.R.drawable.def_img)
                .showImageForEmptyUri(com.xysq.lemon.R.drawable.def_img)
                .showImageOnFail(com.xysq.lemon.R.drawable.def_img)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .discCacheSize(DISK_CACHE_SIZE)
                .defaultDisplayImageOptions(options)
                .build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);
    }

    public static IWXAPI getWXAPI() {
        if (msgApi != null) {
            return msgApi;
        } else {
            throw new IllegalStateException("ImageLoader没有初始化");
        }
    }
    public static ImageLoader getImageLoader() {
        if (imageLoader != null) {
            return imageLoader;
        } else {
            throw new IllegalStateException("ImageLoader没有初始化");
        }
    }
    public DBHelper getDBHelper() {
        if (dbHelper != null) {
            return dbHelper;
        } else {
            throw new IllegalStateException("dbHelper没有初始化");
        }
    }
}